package com.flong.optional;

import org.junit.Test;

import java.util.Optional;

/**
 * @Author:liangjl
 * @Date: 2020/6/21-22:12
 * @Eamil:jilongliang@sina.com
 * @Description:
 */
public class Test1 {

    @Test
    public void test1() {
        //1、Optional的of只能传入具体值，不能传入null
        //2、Optional的ofNullable可以传入具体值，也可以传入null值
        //3、Optional的empty传入的是null
        //4、Optional的isPresent判断Optional是否有具体值，有值就返回true，没有值就返回false

        Optional<String> username = Optional.of("周伯通");
        System.out.println(username.get());

        Optional<Object> empty = Optional.empty();

        if (empty.isPresent()){
            System.out.println("有值");
        }else{
            System.out.println("没有值");
        }

    }

    @Test
    public void test2() {
        Optional<Object> empty = Optional.empty();

        //如果empty没有值就获取orElse的值
        Object username = empty.orElse("周伯通");
        System.out.println(username);
    }

    @Test
    public void test3() {
        Optional<String> username = Optional.of("一灯大师");

        //如果有ifPresent就获取
        username.ifPresent(s->System.out.println(s));
        username.ifPresent(System.out::println);
    }
}
